<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="所属楼栋id"></el-input>
    </el-form-item>
    <el-form-item label="所属楼层id" prop="floorId">
      <el-input v-model="dataForm.floorId" placeholder="所属楼层id"></el-input>
    </el-form-item>
    <el-form-item label="所属房屋id" prop="houseId">
      <el-input v-model="dataForm.houseId" placeholder="所属房屋id"></el-input>
    </el-form-item>
    <el-form-item label="设备类型，如：灭火器、消火栓、消防水泵、消防电梯、自动喷水灭火系统等" prop="type">
      <el-input v-model="dataForm.type" placeholder="设备类型，如：灭火器、消火栓、消防水泵、消防电梯、自动喷水灭火系统等"></el-input>
    </el-form-item>
    <el-form-item label="设备型号" prop="model">
      <el-input v-model="dataForm.model" placeholder="设备型号"></el-input>
    </el-form-item>
    <el-form-item label="生产厂商" prop="manufacturer">
      <el-input v-model="dataForm.manufacturer" placeholder="生产厂商"></el-input>
    </el-form-item>
    <el-form-item label="购买日期" prop="purchaseDate">
      <el-input v-model="dataForm.purchaseDate" placeholder="购买日期"></el-input>
    </el-form-item>
    <el-form-item label="设备有效期" prop="lifespan">
      <el-input v-model="dataForm.lifespan" placeholder="设备有效期"></el-input>
    </el-form-item>
    <el-form-item label="上次检查日期" prop="lastInspectionDate">
      <el-input v-model="dataForm.lastInspectionDate" placeholder="上次检查日期"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="note">
      <el-input v-model="dataForm.note" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          buildingId: '',
          floorId: '',
          houseId: '',
          type: '',
          model: '',
          manufacturer: '',
          purchaseDate: '',
          lifespan: '',
          lastInspectionDate: '',
          note: ''
        },
        dataRule: {
          buildingId: [
            { required: true, message: '所属楼栋id不能为空', trigger: 'blur' }
          ],
          floorId: [
            { required: true, message: '所属楼层id不能为空', trigger: 'blur' }
          ],
          houseId: [
            { required: true, message: '所属房屋id不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '设备类型，如：灭火器、消火栓、消防水泵、消防电梯、自动喷水灭火系统等不能为空', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '设备型号不能为空', trigger: 'blur' }
          ],
          manufacturer: [
            { required: true, message: '生产厂商不能为空', trigger: 'blur' }
          ],
          purchaseDate: [
            { required: true, message: '购买日期不能为空', trigger: 'blur' }
          ],
          lifespan: [
            { required: true, message: '设备有效期不能为空', trigger: 'blur' }
          ],
          lastInspectionDate: [
            { required: true, message: '上次检查日期不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/device/device/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buildingId = data.device.buildingId
                this.dataForm.floorId = data.device.floorId
                this.dataForm.houseId = data.device.houseId
                this.dataForm.type = data.device.type
                this.dataForm.model = data.device.model
                this.dataForm.manufacturer = data.device.manufacturer
                this.dataForm.purchaseDate = data.device.purchaseDate
                this.dataForm.lifespan = data.device.lifespan
                this.dataForm.lastInspectionDate = data.device.lastInspectionDate
                this.dataForm.note = data.device.note
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/device/device/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'buildingId': this.dataForm.buildingId,
                'floorId': this.dataForm.floorId,
                'houseId': this.dataForm.houseId,
                'type': this.dataForm.type,
                'model': this.dataForm.model,
                'manufacturer': this.dataForm.manufacturer,
                'purchaseDate': this.dataForm.purchaseDate,
                'lifespan': this.dataForm.lifespan,
                'lastInspectionDate': this.dataForm.lastInspectionDate,
                'note': this.dataForm.note
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
